import Tools from "../../framework/utils/Tools";

/**
 * 玩家
 */
const {ccclass, property} = cc._decorator;
 
// 状态
var STATE = {
    AWAIT:      0,      // 待机
    MOVE:       1,      // 移动
 };

 // 方向
 var ACT = {
    UP:         0,      // 上
    RIGHT:      2,      // 右
    DOWN:       4,      // 下
    LEFT:       6,      // 左
 };

@ccclass
export default class Role extends cc.Component {


    @property(sp.Skeleton)
    spine:sp.Skeleton = null;

    private state:number      = STATE.AWAIT;      // 状态
    private curAct:number     = ACT.DOWN;         // 方向
    private paths      = [];               // 移动路径队列
    private moveCb     = null;             // 移动结束回调函数
    private curVec2    = null;             // 地图下标s

    //格子大小
    private gap:number = 0;
  
    // 移动速度
    private speed:number = 360;
    

    setGap(gap:number){
        this.gap = gap;
    } 

    init(pos:cc.Vec2){
        this.node.x = pos.x * this.gap ;
        this.node.y = pos.y * this.gap ;  
    }

    // 根据方向选择待机动画
    waitAni () {
        var sp = this.spine;  
        switch( this.curAct ) {
            case ACT.UP: 
                sp.setAnimation( 0, "a_wait2", true );
                break;
            case ACT.DOWN: 
                sp.setAnimation( 0, "a_wait1", true );
                break;
            case ACT.RIGHT:
                sp.node.scaleX = -1;
                sp.setAnimation( 0, "a_wait1", true );
                break;
            case ACT.LEFT:
                sp.node.scaleX = 1;
                sp.setAnimation( 0, "a_wait1", true );
                break;
        }
    }

    setTilePos ( val ) {
        this.curVec2 = new cc.Vec2( val.x ,  val.y  );
    }

    move ( paths, cb ) {
        if ( !Array.isArray( paths ) ) {
            return false;
        }

        this.paths.pop();
        this.moveCb = cb;
        this.paths = paths;
        if ( STATE.AWAIT == this.state ) {
            this.curAct = null;
            this.startMove();
        }
    }

    // 开始移动
    startMove () {
        var path = this.paths.pop();
        if ( Tools.isEmpty( path ) ) {
            return this.moveSucc();
        }
        // 计算方向
        var act = this.figureAct( path);  
        // 保存状态
        this.state = STATE.MOVE; 
        this.setTilePos( path );
        // 行走动画
        this.moveAni( act );

        // 开始移动
        console.log(path.x, path.y)
        cc.tween(this.node).to( this.speed/1000, { position: cc.v3( path.x* 150, path.y* 150 )}, ).call( this.onEndMove.bind(this) ).start();
    }

         // 响应结束移动
    onEndMove () {
        this.state = STATE.AWAIT;
        this.startMove();
    }


    // 根据方向选择行走动画
    moveAni ( act ) {
        if ( this.curAct === act ) return;
        var sp = this.spine; 
        this.curAct = act;
     
        switch( act ) {
            case ACT.UP: 
                sp.setAnimation( 0, "a_run1", true );
                break;
            case ACT.DOWN: 
                sp.setAnimation( 0, "a_run1", true );
                break;
            case ACT.RIGHT:
                sp.node.scaleX = -1;
                sp.setAnimation( 0, "a_run1", true );
                break;
            case ACT.LEFT:
                sp.node.scaleX = 1;
                sp.setAnimation( 0, "a_run1", true );
                break;
        }
    }
 
      // 计算方向
      figureAct ( p ) {
        if ( this.curVec2.x == p.x && this.curVec2.y < p.y ) {
            console.log("上")
            return ACT.UP;
        }
        if ( this.curVec2.x == p.x && this.curVec2.y > p.y ) {
            console.log("下")
            return ACT.DOWN;
        }
        if ( this.curVec2.y == p.y && this.curVec2.x < p.x ) {
            console.log("右")
            return ACT.RIGHT;
        }
        if ( this.curVec2.y == p.y && this.curVec2.x > p.x ) {
            console.log("左")
            return ACT.LEFT;
        }
        return ACT.DOWN;
    }

      // 移动完成
      moveSucc () {
        this.waitAni();
        this.moveCb && this.moveCb();
    }
}
